package com.test.ssh.webapp.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;

import com.opensymphony.xwork2.ActionContext;
import com.test.ssh.model.User;
import com.test.ssh.service.UserManager;

@Results(
		{
			@Result(name="index",value="/index.jsp"),
			@Result(name="input",value="/login.jsp")
			
		}
		)
public class LoginAction {
	private UserManager userManager ;
	private String username ;
	private String password ;
	
	public String execute() {
		List<User> l = userManager.findByHql("from User where username=?", username);
		if(l.size()>0){
			User u = l.get(0);
			if(u.getPassword().equals(password)){
				((Map)ActionContext.getContext().get("session")).put("currentUser",u) ;
				ServletActionContext.getRequest().getSession().setAttribute("loginuser",u) ;
				return "index" ;
			}
		}
		return "input" ;
	}
	
	public String out() {
		ServletActionContext.getRequest().getSession().removeAttribute("loginuser") ;
		return "index" ;
	}
	public UserManager getUserManager() {
		return userManager;
	}
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}
